#ifndef _DIM_SUM_EXEC_H
#define _DIM_SUM_EXEC_H

#include <dim-sum/types.h>
#include <dim-sum/elf.h>

#define NEW_RUNBIN_STATE (0)
#define OLD_EXECVE_STATE (1)

#define BINPRM_BUF_SIZE 128
#define RAND_CHAR_SIZE (16)
#define AUXV_NR (10)

#define ARG_STRLEN_MAX (PAGE_SIZE * 32)
#define ARG_STRINGS_MAX (0x7FFFFFFF)

#define NEW_AUX_ENT(p, type, val) \
	do { \
		(p)->a_type = type; \
		(p)->a_un.a_val = val; \
	} while (0)

struct arg_env_info {
	
	char * const *uargv_p;
	char * const *uenvp_p;
	void* ustart;
	void* unext;
	void* utop;

	char * const *app_argv;
	char * const *app_envp;
	long app_argv_count;
	long app_envp_count;
	long write_user_size;

	int fd;
	int script_fd;
	char *filename;
	long filename_sz;
	char *script_name;
	long script_name_sz;
	char *script_arg;
	long script_arg_sz;
	
	char ** k_argv_spot;	
	long k_argv_spot_max;	
	char ** k_envp_spot;
	long k_envp_spot_max;	
	unsigned char* k_rand_arr;
	long k_rand_arr_sz;	
	struct elf_auxv* k_auxv_arr;
	long k_auxv_arr_sz;

};

struct exec_info {
	/**
	 * 目前仅仅包含环境变量和命令行参数相关的信息
	 * 后面会有扩展
	*/
	struct arg_env_info ae_info;
	void* user_argenv_sp;
	struct elfhdr* elf_hdr;
	void* exe_phdr;
	unsigned long phdr_nr;
	unsigned long load_addr;
	unsigned long interp_load_addr;
	unsigned long entry;
};
char *__get_task_name(char *buf, size_t buf_size, struct task_desc *tsk);
void __set_task_name(struct task_desc *tsk, const char *buf, bool exec);
int vmem_execve(const char *filename, char * const *argv,
					char * const *envp);
int task_execve(const char *filename, char * const *argv, 
						char * const *envp, 
						struct exception_spot* regs);
int exit_execv_info(struct task_desc* t);
#endif